/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.gps;

import android.content.Context;
import android.content.Intent;
import android.location.Location;

import id.duapuluhdua.dakidaki.ServiceDataReceiver;

/**
 *
 */
public class GpsLocation
{
  /** Fields */
  private static String appNameKey = "DakiDaki";
  private static String appNameValue = "GpsLocation";

  public long pointId;
  public int saveMode;

  public double wgsLon;
  public double wgsLat;

  public int altitude;
  public int accuracy;
  public int bearing;

  public long timeUtc;

  public boolean bEnabled;

  /**
   * Initializes the GPS location info.
   */
  public GpsLocation()
  {
    clear();
  }

  /**
   * Reset the GPS location data.
   */
  public void clear()
  {
    pointId = -1;
    saveMode = GpsLogger.MODE_NONE;

    wgsLon = 0;
    wgsLat = 0;

    altitude = 0;
    accuracy = 0;
    bearing = 0;

    timeUtc = 0;

    bEnabled = false;
  }

  /**
   * Returns the status of this object. It gets enabled if the data is
   * initialized, either by an Intent, Location or GPSLocation object.
   *
   * @return true if the object is enabled, or false otherwise.
   */
  public boolean isEnabled()
  {
    return bEnabled;
  }

  /**
   * Sends the new location to the context parent.
   *
   * @param context
   *            The application context.
   */
  public void send(Context context)
  {
    final Intent intent = new Intent(context, ServiceDataReceiver.class);

    intent.putExtra(appNameKey, appNameValue);

    intent.putExtra("lPointID", pointId);
    intent.putExtra("iSaveMode", saveMode);

    intent.putExtra("dLongitude", wgsLon);
    intent.putExtra("dLatitude", wgsLat);

    intent.putExtra("iAltitude", altitude);
    intent.putExtra("iAccuracy", accuracy);
    intent.putExtra("iBearing", bearing);

    intent.putExtra("lTimeUTC", timeUtc);

    context.sendOrderedBroadcast(intent, null);
  }

  /**
   * Copy the GPS information from src into this GpsLocation object.
   *
   * @param src
   *            The source copy.
   * @return true if the copy is successful, or false otherwise.
   */
  public boolean set(GpsLocation src)
  {
    if (src != null) {
      bEnabled = true;

      pointId = src.pointId;
      saveMode = src.saveMode;

      wgsLon = src.wgsLon;
      wgsLat = src.wgsLat;

      altitude = src.altitude;
      accuracy = src.accuracy;
      bearing = src.bearing;

      timeUtc = src.timeUtc;

      return true;
    }
    return false;
  }

  /**
   * Copy the GPS information from src into this GpsLocation object.
   * Additional information is added such as the point id and the save mode.
   *
   * @param loc
   *            The geographic location object.
   * @param locationPointId
   *            The point id.
   * @param locationSaveMode
   *            The save mode.
   * @return true if the copy is successful, or false otherwise.
   */
  public boolean set(Location loc, long locationPointId, int locationSaveMode)
  {
    if (loc != null) {
      bEnabled = true;

      pointId = locationPointId;
      saveMode = locationSaveMode;

      wgsLon = loc.getLongitude();
      wgsLat = loc.getLatitude();

      altitude = (int)loc.getAltitude();
      accuracy = (int)loc.getAccuracy();
      bearing = (int)loc.getBearing();

      timeUtc = loc.getTime();

      return true;
    }
    return false;
  }

  /**
   * Copy the GPS information from the intent into this GpsLocation object.
   *
   * @param intent the Intent object.
   * @return true if the copy is successful, or false otherwise.
   */
  public boolean get(Intent intent)
  {
    if ((intent != null) && (intent.hasExtra(appNameKey))) {
      final String sNewAppNameValue = intent.getStringExtra(appNameKey);
      if (sNewAppNameValue != null) {
        if (sNewAppNameValue.equals(appNameValue)) {
          bEnabled = true;

          pointId = intent.getLongExtra("lPointID", -1);
          saveMode = intent.getIntExtra("iSaveMode", GpsLogger.MODE_NONE);

          wgsLon = intent.getDoubleExtra("dLongitude", 0);
          wgsLat = intent.getDoubleExtra("dLatitude", 0);

          altitude = intent.getIntExtra("iAltitude", 0);
          accuracy = intent.getIntExtra("iAccuracy", 0);
          bearing = intent.getIntExtra("iBearing", 0);

          timeUtc = intent.getLongExtra("lTimeUTC", 0);

          return true;
        }
      }
    }
    return false;
  }
}
